package com.itheima;


import com.itheima.entity.ValidateNote;
import com.itheima.exception.BusinessException;
import com.itheima.utils.ValidationUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class ResultResponseAspect {

    @Pointcut("execution(public * com.itheima.controller.*.*(..))")
    public void getPuduct(){}

    @Before(value = "getPuduct()")
    public void before(JoinPoint pjp){
        Object[] args = pjp.getArgs();
        HttpServletRequest httpServletRequest;
        for (Object arg : args) {
            if(arg instanceof ValidateNote) {
                ValidationUtil.ValidResult result = ValidationUtil.validateBean(arg);
                if (result.hasErrors()) {
                    throw new BusinessException(result.getErrors());
                }
            }
        }




    }


}
